<?php


namespace app\admin\controller;


use app\BaseController;
use think\Exception;

class Goods extends BaseController
{
    public function index()
    {

        $title = input('title','','trim');

        $data = [];
        if (!empty($title)){
            $data['title'] = $title;
        }


        $goods_list = (new \app\common\business\Goods())->getNormalGoods($data);

        return view('',[
            'goods_list' =>$goods_list
        ]);
    }

    public function add()
    {
        return view();
    }

    public function save()
    {
        if (!request()->isPost()) {
            return show(2, '请求不合法');
        }

        if (!request()->checkToken('__token__')){
            return show(2,'非法请求');
        }

        $data = input('param.');

        //基于数据验证成功后
        $data['category_path_id'] = $data['category_id'];
        $res = explode(',', $data['category_path_id']);
        $data['category_id'] = end($res);

        try {
            $res = (new \app\common\business\Goods())->insertData($data);
        } catch (Exception $e) {
            return show(2, $e->getMessage());
        }

        if (!$res) {
            return show(2, '新增失败');
        }

        return show(1, 'ok');

    }
}